using System;
using System.Runtime.InteropServices;

namespace CMS.Utils.IpSerials.Security
{
	public class IpKey
	{
		[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
		internal struct SYSTEM_INFO
		{
			public  int  dwOemId;
			public  int  dwPageSize;
			public  int  lpMinimumApplicationAddress;
			public  int  lpMaximumApplicationAddress;
			public  int  dwActiveProcessorMask;
			public  int  dwNumberOfProcessors;
			public  int  dwProcessorType;
			public  int  dwAllocationGranularity;
			public  int  dwProcessorLevel;
			public  int  dwProcessorRevision;    
		}
		[DllImport("kernel32.dll")]
		internal static extern void GetSystemInfo (ref SYSTEM_INFO lpSystemInfo);
        public static bool CheckComputerKey(string SerialNumber, string computerKey)
        {
            if (SerialNumber.Length < 10)
                return false;
            SerialNumber = SerialNumber.Substring(9);
            SerialNumber = SerialNumber.Replace("-", "");
            Security.Encrypt.DeEncryption(SerialNumber, ref SerialNumber);
            string cdkey = "LCWE-2013" + FormatKey(SerialNumber);
            //string key = GetComputerKey();
            if (computerKey.Equals(cdkey))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cdkey"></param>
        /// <returns></returns>
        internal static string GetSerialNumber(string sComputerKey)
        {
            if (sComputerKey.Length < 10)
                return "";

            sComputerKey = sComputerKey.Substring(9);
            sComputerKey = sComputerKey.Replace("-", "");
            Security.Encrypt.Encryption(sComputerKey, ref sComputerKey);
            string SerialNumber = "LCWE-2013" + FormatKey(sComputerKey);
            return SerialNumber;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetComputerKey()
        {
            SYSTEM_INFO info = new SYSTEM_INFO();
            GetSystemInfo(ref info);
            string key = "";// DateTime.Now.Second.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString();
            key += info.dwProcessorLevel.ToString();
            //key += DateTime.Now.Millisecond.ToString();
            key += info.dwProcessorRevision.ToString();
            key += info.dwNumberOfProcessors.ToString();
            key += info.dwProcessorType.ToString();
            //key += DateTime.Now.Millisecond.ToString();
            key += info.dwActiveProcessorMask.ToString();
            key += info.dwAllocationGranularity.ToString();
            if (key.Length <= 12)
            {
                while (key.Length < 12)
                {
                    key += "0";
                }
            }
            else
            {
                key = key.Substring(0, 12);
            }
            Security.Encrypt.Encryption(key, ref key);
            key = "LCWE-2013" + FormatKey(key);
            return key;
        }
		private static string FormatKey(string key)
		{
			string cdkey = "";
			while(key != "" )
			{
				if(key.Length > 4)
				{
					cdkey+= "-" + key.Substring(0, 4);
					key = key.Substring(4);
				}
				else
				{
					cdkey+= "-" + key;
					key = "";
				}
			}
			return cdkey;
		}
	}
}
